package com.alex.service.r;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Entity;
import cn.hutool.http.HttpUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alex.common.config.YMLEntity;
import com.alex.database.entity.PmUtAidInfo;
import com.alex.database.entity.Pmr;
import com.alex.database.entity.SpiderTbTrue;
import com.alex.database.mapper.PmUtAidInfo10wMapper;
import com.alex.database.mapper.PmrMapper;
import com.alex.database.mapper.SpiderTbTrueMapper;
import com.alex.service.r.rentity.Conceptualstructuremap;
import com.alex.service.r.rentity.ConceptualstructuremapParseFront;
import com.alex.service.r.rentity.ListMostObject;
import com.alex.service.r.rentity.MostObject;
import com.alex.utils.CacheUtil;
import com.alex.utils.CheckUtil;
import com.alex.utils.MyUtils;
import com.alex.utils.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * @Author: gcr
 * @Date: 2023/11/29 16:22
 */
@Service
@Slf4j
public class Rservice {
    // 本程序主要处理R 语言用

    public static String RESTURL = "http://10.1.1.11:65213/";

    @Resource
    PmrMapper pmrMapper;

    @Resource
    PmUtAidInfo10wMapper pmUtAidInfo10wMapper;

    @Resource
    CacheUtil cacheUtil;

    @Resource
    AsyncService asyncService;

    @Resource
    SpiderTbTrueMapper spiderTbTrueMapper;


    @Resource
    YMLEntity ymlEntity;

    // 获取此路径下全部图片文件
    public static List<File> getImageAll(String path) {
        List<File> fLs = new ArrayList<>();
        File[] ls = FileUtil.ls(path);
        for (File l : ls) {
            if (l.getName().endsWith(".png") || l.getName().endsWith(".tiff")) {
                fLs.add(l);
            }
        }
        System.out.println("路径下+" + path + "+图片数量++" + fLs.size());
        List<File> collect = fLs.stream().sorted(Comparator.comparing(String::valueOf)).collect(Collectors.toList());
        return collect;
    }

    // 获取此路径下全部html
    public static List<File> getHtmlAll(String path) {
        List<File> fLs = new ArrayList<>();
        File[] ls = FileUtil.ls(path);
        for (File l : ls) {
            if (l.getName().endsWith(".html") || l.getName().endsWith(".htm")) {
                fLs.add(l);
            }
        }
        System.out.println("路径下+" + path + "+html数量++" + fLs.size());
        List<File> collect = fLs.stream().sorted(Comparator.comparing(String::valueOf)).collect(Collectors.toList());
        return collect;
    }

    /**
     * 详细处理返回的 id 集合
     *
     * @return
     */
    public String dealUts(List<Integer> res, String term, Integer uid) {
        Integer userUid = uid;
        List<String> uts = new ArrayList<>();
        for (Integer re : res) {
            PmUtAidInfo pmUtAidInfo10w = pmUtAidInfo10wMapper.selectById(re);
            if (pmUtAidInfo10w != null) {
                boolean isadd = false;
                if (StringUtil.isNotNull(pmUtAidInfo10w.getPmId()) && pmUtAidInfo10w.getPmId() != 0) {
                    isadd = true;
                    uts.add(pmUtAidInfo10w.getPmId() + "");
                }
                if (!isadd && StringUtil.isNotNull(pmUtAidInfo10w.getUt())) {
                    uts.add(pmUtAidInfo10w.getUt());
                }
            }

        }
//        if (StringUtil.isNotNull(res)) {
//            JSONObject jsonObject = JSONObject.parseObject(res);
//            if (jsonObject.get("code").toString().equals("0")) {
//                JSONArray data = jsonObject.getJSONArray("data");
//                for (Object datum : data) {
//                    uts.add(datum.toString());
//                }
//            }
//        }
        //
        CheckUtil.check(uts.size() != 0, "暂无数据");
        String Db = ymlEntity.getScholar();
        log.info("db++" + Db);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("table", Db);
        String join = CollUtil.join(uts, ",");
//        log.info("join+" + join);
        paramMap.put("pmids", join);
        log.info("join+" + join);
        List<Entity> entities = new ArrayList<>();
        try {
            String post = HttpUtil.post("https://pminfo.yuntsg.com/index/get-spider-tb-true", paramMap, 25000);
            entities = JSON.parseArray(post, Entity.class);
        } catch (Exception e) {

            e.printStackTrace();

        }
        String term_md5 = MyUtils.GetMD5Code(term);


        String redisKey = userUid + term_md5;
        log.info("进行wos pmid转化进行wos 画图");
        term_md5 = term_md5 + "sc";
        log.info("新生成的wosmd5+" + term_md5);
        redisKey = redisKey + "parse";
        log.info("新生成的wosredisKey+" + redisKey);
        String fn = System.currentTimeMillis() + ".txt";
        String rootPathTemp = "/home/pubmedr/temp/";
        log.info("下载成功pmid文件wos 文档数量是" + entities.size());
        CheckUtil.check(entities.size() != 0, "暂无数据");
        geneWosExportFile(entities, rootPathTemp, fn);
        if (FileUtil.exist(rootPathTemp + fn)) {
            File file = new File(rootPathTemp + fn);
            String s = searchRightGenerateRtiff("", file, null, term_md5, uid);
            return s;
        } else {
            cacheUtil.saveObjectByRedis(redisKey, 3);
            return redisKey;
        }
    }

    /**
     * R 代码生成
     */
    @SneakyThrows
    public String searchRightGenerateRtiff(String term, File wosfile, Integer flag, String termMD5, Integer uid) {
        Integer userUid = uid;
        // 判断如果同一天一个人检索同一个词直接返回全部数据
        List<String> resultList = new LinkedList<>();
        String rootPath = "/home/pubmedr/";
        Map<Integer, String> msg = new HashMap<>();
        msg.put(1, "进行中");
        msg.put(2, "成功");
        msg.put(3, "失败,请刷新页面");
        msg.put(4, "已过期,请重新刷新页面");
        // 这个打算才用redis 与线程结合方式 不堵塞主线程 防止出现卡死状态
        // 2023年11月29日16:51:48 wos 与pm 结合 才用和wos 查询模式
        // 这里是wos文件解析
        // 修正这个里面采用rekey 进行存换
        log.info("wos文件解析");
        String term_md5 = "";
        String FileStr = FileUtil.readUtf8String(wosfile);
        if (StringUtil.isNotNull(termMD5)) {
            term_md5 = termMD5;
        }
        if (StringUtil.isNull(term_md5)) {
            CheckUtil.check(false, "文件有错误");
        }
        String redisKey = userUid + term_md5;
        log.error("wos 文件生成rekey++" + redisKey);
        String redisKeyRes = userUid + term_md5 + "res";
        Object object = cacheUtil.getObjectByRedis(redisKey);
        if (object == null) {
            cacheUtil.saveObjectByRedis(redisKey, 1);
        }
        List<Pmr> pmrs = pmrMapper.selectList(new QueryWrapper<Pmr>().eq("uid", userUid).eq("term_md5", term_md5)
                .ne("status", 4));
        boolean isgO = pmrs.size() <= 1;
        CheckUtil.check(isgO, "请等待结果");
        log.info("pmrs+size+" + pmrs.size());
        if (pmrs.size() > 0) {
            Pmr pmr = pmrs.get(0);
            log.info("当前状态++" + pmr.getStatus());
            if (pmr.getStatus() == 2) {
                List<String> strings = JSONObject.parseArray(pmr.getRes(), String.class);
                boolean isCorrect = true;
                String path = pmr.getPath();
                if (!FileUtil.exist(path)) {
                    pmr.setStatus(4);
                    pmrMapper.updateById(pmr);
                    isCorrect = false;
                }
                if (!isCorrect) {
                    cacheUtil.saveObjectByRedis(redisKey, 4);
                    return redisKey;
                } else {
                    cacheUtil.saveObjectByRedis(redisKey, 2);
                    cacheUtil.saveObjectByRedis(redisKeyRes, JSONObject.toJSONString(strings));
                    cacheUtil.saveObjectByRedis(redisKey + "path", pmr.getPath());
                    return redisKey;
                }
            } else {
                log.info("点击到这里了 查询进度");
                if (pmr.getStatus() != null && (pmr.getStatus() == 3 || pmr.getStatus() == 1)) {
                    if (StringUtil.isNotNull(pmr.getPath())) {
                        CheckUtil.check(false, "进行中");
                    }
                } else {
                    cacheUtil.saveObjectByRedis(redisKey, pmr.getStatus());
                    return redisKey;
                }
            }

        } else {
            log.info("wos异步开始");
            asyncService.dealWosProgressByClick(FileStr, rootPath, userUid, term, term_md5, resultList);
//            cacheUtil.saveObjectByRedis(redisKey, 1);
            return redisKey;

        }
        return "";

    }

    /**
     * 根据前台传进来的数据进行合并转化成标准检索
     *
     * @param param
     * @return "id=85126674,48943338,60808863,78207656,72032590" 指的 article_id
     */
    public String pmUTbackSearch(String param) {
        String res = "";
        String[] split = param.split(",");
        List<String> quParam = new ArrayList<>();
        for (String s : split) {
            if (s.toLowerCase().startsWith("wos")) {
                //进行转化
                List<SpiderTbTrue> ut = spiderTbTrueMapper.selectList(new QueryWrapper<SpiderTbTrue>().eq("UT", s.toUpperCase()));
                if (ut.size() > 0) {
                    SpiderTbTrue spiderTbTrue = ut.get(0);
                    if (StringUtil.isNotNull(spiderTbTrue.getPm())) {
                        quParam.add(spiderTbTrue.getPm());
                    }
                }
            } else {
                quParam.add(s);
            }
        }
        //获取数据
        Map<String, Object> map = new HashMap<>();
        map.put("utpm", String.join(",", quParam));
        String result = HttpUtil.post(RESTURL + "article/get-r-utpmlist", map);

        if (StringUtil.isNotNull(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.get("code").toString().equals("0")) {
                res = jsonObject.getString("data");

            }
        }

        return res;
    }

    // 全部从原来的 pm 转ut 或者转成张福磊自己主见id
    public Object testConceptualstructuremap(String rekey) {
        String path = "/home/pubmedr/R/alldata1.xlsx";
        if (StringUtil.isNotNull(rekey)) {
            String redisPath = getRedisPath(rekey);
            if (StringUtil.isNotNull(redisPath)) {
                path = path.replace("/home/pubmedr/R/", redisPath);
            } else {
                log.error("使用了默认路径++" + path);
            }
        }
        List<Conceptualstructuremap> resList = new ArrayList<>();
        List<ConceptualstructuremapParseFront> resRes = new ArrayList<>();
//        List<MostObject> resList = new ArrayList<>();
        if (FileUtil.exist(path)) {
            ExcelReader read = ExcelUtil.getReader(path);
            List<Map<String, Object>> maps = read.readAll();
            //2023年12月9日10:23:30 留下了技术太菜的泪水 如果R 语言精通也不会这个菜 用java 处理了 加油吧
            Set<String> excludeKey = Stream.of("Dim.1", "Dim.2", "clust", "shape", "label", "color", "keys").collect(Collectors.toSet());
            for (Map<String, Object> res : maps) {
                Conceptualstructuremap conceptualstructuremap = new Conceptualstructuremap();
                Set<String> strings = res.keySet();
                conceptualstructuremap.setX(Double.valueOf(res.get("Dim.1").toString()));
                conceptualstructuremap.setY(Double.valueOf(res.get("Dim.2").toString()));
                conceptualstructuremap.setClust(Integer.parseInt(res.get("clust").toString()));
                conceptualstructuremap.setLabel(res.get("label").toString());
                conceptualstructuremap.setColor(res.get("color").toString());
                List<String> pmids = new ArrayList<>();
                for (String string : strings) {
                    try {
                        if (!excludeKey.contains(string)) {
                            Integer flag = Integer.parseInt(res.get(string).toString());
                            // chimal-monroy j, 1999, int j dev biol pmid:10213083
                            if (flag == 1) {
                                String pmid = (string.substring(string.indexOf("pmid:") + 5));
                                pmids.add(pmid);
                            }
                        }
                    } catch (Exception e) {
//                    e.printStackTrace();
                    }

                }
                conceptualstructuremap.setPmids(pmids);
                resList.add(conceptualstructuremap);

            }
            // 2023年12月11日14:45:35 转成需要的前段格式

            Map<Integer, List<Conceptualstructuremap>> temp = new HashMap<>();
            for (Conceptualstructuremap conceptualstructuremap : resList) {
                Integer clust = conceptualstructuremap.getClust();
                temp.compute(clust, (k, v) -> {
                    if (v == null) {
                        v = new ArrayList<>();
                    }
                    v.add(conceptualstructuremap);
                    return v;
                });
            }
            temp.forEach((k, v) -> {
                ConceptualstructuremapParseFront conceptualstructuremapParseFront = new ConceptualstructuremapParseFront();
                conceptualstructuremapParseFront.setClust(k);
                Conceptualstructuremap conceptualstructuremap = v.get(0);
                conceptualstructuremapParseFront.setColor(conceptualstructuremap.getColor());
                List<ConceptualstructuremapParseFront.data> as = new ArrayList<>();
                for (Conceptualstructuremap conceptualstructuremap1 : v) {
                    ConceptualstructuremapParseFront.data data = new ConceptualstructuremapParseFront.data();
                    data.setX(conceptualstructuremap1.getX());
                    data.setY(conceptualstructuremap1.getY());
                    data.setName(conceptualstructuremap1.getLabel());
                    data.setPmids(conceptualstructuremap1.getPmids());
                    as.add(data);
                }
                conceptualstructuremapParseFront.setLabel(as);
                resRes.add(conceptualstructuremapParseFront);
            });

        }
        return resRes;

    }

    public List<Conceptualstructuremap> FactorialMapOfTheDocumentsWithTheHighestContributes(String rekey) {
        String path = "/home/pubmedr/R/Factorial map of the documents.xlsx";
        if (StringUtil.isNotNull(rekey)) {
            String redisPath = getRedisPath(rekey);
            if (StringUtil.isNotNull(redisPath)) {
                path = path.replace("/home/pubmedr/R/", redisPath);
            } else {
                log.error("使用了默认路径++" + path);
            }
        }
        List<Conceptualstructuremap> resList = new ArrayList<>();
        if (FileUtil.exist(path)) {
            ExcelReader read = ExcelUtil.getReader(path);
            List<Map<String, Object>> maps = read.readAll();
            //2023年12月9日10:23:30 留下了技术太菜的泪水 如果R 语言精通也不会这个菜 用java 处理了 加油吧

            for (Map<String, Object> res : maps) {
                Conceptualstructuremap conceptualstructuremap = new Conceptualstructuremap();
                conceptualstructuremap.setX(Double.valueOf(res.get("dim1").toString()));
                conceptualstructuremap.setY(Double.valueOf(res.get("dim2").toString()));
                conceptualstructuremap.setClust(Integer.parseInt(res.get("center cluster").toString().replace("cluster", "")));
                String nomi = res.get("nomi").toString();
                if (nomi.contains("pmid")) {
                    nomi = nomi.substring(0, nomi.indexOf("pmid"));
                }
                conceptualstructuremap.setLabel(nomi);
                conceptualstructuremap.setColor(res.get("center color").toString());
                List<String> pmids = new ArrayList<>();
                try {
                    String string = res.get("nomi").toString();
                    // chimal-monroy j, 1999, int j dev biol pmid:10213083
                    String pmid = (string.substring(string.indexOf("pmid:") + 5));
                    pmids.add(pmid);

                } catch (Exception e) {
//                    e.printStackTrace();
                }
                conceptualstructuremap.setPmids(pmids);
                resList.add(conceptualstructuremap);
            }
        }


        return resList;

    }

    public List<MostObject> MostProdCountriesdf(String rekey) {
        String path = "/home/pubmedr/R/MostProdCountriesdf.xlsx";
        if (StringUtil.isNotNull(rekey)) {
            String redisPath = getRedisPath(rekey);
            if (StringUtil.isNotNull(redisPath)) {
                path = path.replace("/home/pubmedr/R/", redisPath);
            } else {
                log.error("使用了默认路径++" + path);
            }
        }
        List<MostObject> resList = new ArrayList<>();
        if (FileUtil.exist(path)) {
            ExcelReader read = ExcelUtil.getReader(path);
            List<Map<String, Object>> maps = read.readAll();
            for (Map<String, Object> res : maps) {
                MostObject mostObject = new MostObject();
                mostObject.setLable(res.get("Country").toString());
                mostObject.setSize(Integer.parseInt(res.get("Articles").toString()));
                mostObject.setPmids(getPmids(res.get("pminfo").toString()));
                resList.add(mostObject);
            }
        }
        return resList;

    }

    public List<MostObject> MostCitedPapersDATA(String rekey) {
        String path = "/home/pubmedr/R/MostCitedPapersDATA.xlsx";
        if (StringUtil.isNotNull(rekey)) {
            String redisPath = getRedisPath(rekey);
            if (StringUtil.isNotNull(redisPath)) {
                path = path.replace("/home/pubmedr/R/", redisPath);
            } else {
                log.error("使用了默认路径++" + path);
            }
        }
        List<MostObject> resList = new ArrayList<>();
        if (FileUtil.exist(path)) {
            ExcelReader read = ExcelUtil.getReader(path);
            List<Map<String, Object>> maps = read.readAll();
            for (Map<String, Object> res : maps) {
                MostObject mostObject = new MostObject();
                mostObject.setLable(res.get("Paper").toString());
                mostObject.setSize(Integer.parseInt(res.get("TC").toString()));
                mostObject.setPmids(getPmids(res.get("PMID").toString()));
                resList.add(mostObject);
            }
        }
        return resList;


    }

    public List<MostObject> MostRelSourcesDATA(String rekey) {

        String path = "/home/pubmedr/R/MostRelSourcesDATA.xlsx";
        if (StringUtil.isNotNull(rekey)) {
            String redisPath = getRedisPath(rekey);
            if (StringUtil.isNotNull(redisPath)) {
                path = path.replace("/home/pubmedr/R/", redisPath);
            } else {
                log.error("使用了默认路径++" + path);
            }
        }
        List<MostObject> resList = new ArrayList<>();
        if (FileUtil.exist(path)) {
            ExcelReader read = ExcelUtil.getReader(path);
            List<Map<String, Object>> maps = read.readAll();
            for (Map<String, Object> res : maps) {
                MostObject mostObject = new MostObject();
                mostObject.setLable(res.get("SO").toString());
                mostObject.setSize(Integer.parseInt(res.get("Articles").toString()));
                mostObject.setPmids(getPmids(res.get("PMIDS").toString()));
                resList.add(mostObject);
            }
        }
        return resList;

    }

    public List<MostObject> TCperCountries(String rekey) {
        String path = "/home/pubmedr/R/TCperCountriesData.xlsx";
        if (StringUtil.isNotNull(rekey)) {
            String redisPath = getRedisPath(rekey);
            if (StringUtil.isNotNull(redisPath)) {
                path = path.replace("/home/pubmedr/R/", redisPath);
            } else {
                log.error("使用了默认路径++" + path);
            }
        }
        List<MostObject> resList = new ArrayList<>();
        if (FileUtil.exist(path)) {
            ExcelReader read = ExcelUtil.getReader(path);
            List<Map<String, Object>> maps = read.readAll();
            for (Map<String, Object> res : maps) {
                MostObject mostObject = new MostObject();
                mostObject.setLable(res.get("Country").toString());
                mostObject.setSize(Integer.parseInt(res.get("totalc").toString()));
                mostObject.setPmids(getPmids(res.get("pminfo").toString()));
                resList.add(mostObject);
            }
        }
        return resList;

    }

    public List<MostObject> AuthorKeywordsDE(String rekey) {
        String path = "/home/pubmedr/R/AuthorKeywordsDE.xlsx";
        if (StringUtil.isNotNull(rekey)) {
            String redisPath = getRedisPath(rekey);
            if (StringUtil.isNotNull(redisPath)) {
                path = path.replace("/home/pubmedr/R/", redisPath);
            } else {
                log.error("使用了默认路径++" + path);
            }
        }
        List<MostObject> resList = new ArrayList<>();
        if (FileUtil.exist(path)) {
            ExcelReader read = ExcelUtil.getReader(path);
            List<Map<String, Object>> maps = read.readAll();
            for (Map<String, Object> res : maps) {
                MostObject mostObject = new MostObject();
                mostObject.setLable(res.get("DES").toString());
                mostObject.setSize(Integer.parseInt(res.get("n").toString()));
                mostObject.setPmids(getPmids(res.get("pmids").toString()));
                resList.add(mostObject);
            }
        }


        return resList;

    }

    public List<MostObject> KeywordsPlusID(String rekey) {
        String path = "/home/pubmedr/R/KeywordsPlusID.xlsx";
        List<MostObject> resList = new ArrayList<>();
        if (StringUtil.isNotNull(rekey)) {
            String redisPath = getRedisPath(rekey);
            if (StringUtil.isNotNull(redisPath)) {
                path = path.replace("/home/pubmedr/R/", redisPath);
            } else {
                log.error("使用了默认路径++" + path);
            }
        }
        if (FileUtil.exist(path)) {
            ExcelReader read = ExcelUtil.getReader(path);
            List<Map<String, Object>> maps = read.readAll();
            for (Map<String, Object> res : maps) {
                MostObject mostObject = new MostObject();
                mostObject.setLable(res.get("iDS").toString());
                mostObject.setSize(Integer.parseInt(res.get("n").toString()));
                mostObject.setPmids(getPmids(res.get("pmids").toString()));
                resList.add(mostObject);
            }
        }
        return resList;

    }

//    public LinksCategorisPmid AUcollaboration(String rekey) {
//
//
//        String path = "/home/pubmedr/R/MAUCOM.xlsx";
//        List<MostObject> resList = new ArrayList<>();
//        List<ListMostObject> lres = new ArrayList<>();
//        if (StringUtil.isNotNull(rekey)) {
//            String redisPath = getRedisPath(rekey);
//            if (StringUtil.isNotNull(redisPath)) {
//                path = path.replace("/home/pubmedr/R/", redisPath);
//            } else {
//                log.error("使用了默认路径++" + path);
//            }
//        }
//        if (FileUtil.exist(path)) {
//            ExcelReader read = ExcelUtil.getReader(path);
//            List<Map<String, Object>> maps = read.readAll();
//            log.info("获取的元数据大小" + maps.size());
//            // 作者与pmid 集合
//            Map<String, Set<Integer>> authorSetMap = new HashMap<>();
//            // 组内作者两两组合 与pmid 集合
//            Map<AuthorPairs, Set<Integer>> authorPairsSetMap = new HashMap<>();
//            Map<String, Set<Integer>> auPairStringSetMap = new HashMap<>();
//            log.info("作者开始");
//            for (Map<String, Object> res : maps) {
//                Object auparam = res.get("AU");
//                Object pmparam = res.get("PM");
//                if (StringUtil.isNotNull(auparam) && StringUtil.isNotNull(pmparam)) {
//                    Integer pmid = Integer.parseInt(pmparam.toString());
//                    String aus = auparam.toString();
//                    List objects = Arrays.asList(StrUtil.split(aus, ";"));
//                    for (int i = 0; i < objects.size(); i++) {
//                        Author au1 = new Author();
//                        au1.setName(objects.get(i).toString());
//                        String au = au1.getName().trim();
//                        if (authorSetMap.containsKey(au)) {
//                            authorSetMap.get(au).add(pmid);
//                        } else {
//                            Set<Integer> integerSet = new HashSet<>();
//                            integerSet.add(pmid);
//                            authorSetMap.put(au, integerSet);
//                        }
//                        for (int j = 1; j < objects.size(); j++) {
//                            Author au2 = new Author();
//                            au2.setName(objects.get(j).toString());
//                            if (au1.getName().toLowerCase().trim().equals(au2.getName().toLowerCase().trim())) {
//                                continue;
//                            }
//                            AuthorPairs authorPairs = new AuthorPairs(au1.getName(), au2.getName());
//                            if (authorPairsSetMap.containsKey(authorPairs)) {
//                                authorPairsSetMap.get(authorPairs).add(pmid);
//                            } else {
//                                Set<Integer> integerSet = new HashSet<>();
//                                integerSet.add(pmid);
//                                authorPairsSetMap.put(authorPairs, integerSet);
//                            }
//                        }
//                    }
//                }
//            }
//            log.info("数组组合完毕++++");
//            Map<String, Integer> nameid = new HashMap<>();
//            Map<Integer, String> idname = new HashMap<>();
//            List<String> datalist = new ArrayList<>();
//            Integer num = 0;
//            for (Map.Entry<AuthorPairs, Set<Integer>> authorPairsSetEntry : authorPairsSetMap.entrySet()) {
//                String au1 = authorPairsSetEntry.getKey().getFirstAu();
//                String au2 = authorPairsSetEntry.getKey().getSecondAu();
//                String left = "";
//                if (nameid.containsKey(au1)) {
//                    left = nameid.get(au1) + "";
//                } else {
//                    left = num + "";
//                    nameid.put(au1, num);
//                    idname.put(num, au1);
//                    num++;
//                }
//                String right = "";
//                if (nameid.containsKey(au2)) {
//                    right = nameid.get(au2) + "";
//                } else {
//                    right = num + "";
//                    nameid.put(au2, num);
//                    idname.put(num, au2);
//                    num++;
//                }
//                datalist.add(left + "\t" + right);
//            }
//
//            Louvain a = new Louvain();
//            a.init(idname.size(), datalist.size(), datalist);
//            try {
//                a.louvain();
//            } catch (Exception e) {
//
//            }
//            List<List<String>> allasslist = new ArrayList<>();
//            try {
//                ArrayList list[] = new ArrayList[a.global_n];
//                for (int i = 0; i < a.global_n; i++) {
//                    list[i] = new ArrayList<Integer>();
//                }
//                for (int i = 0; i < a.global_n; i++) {
//                    list[a.global_cluster[i]].add(i);
//                }
//                for (int i = 0; i < a.global_n; i++) {
//                    if (list[i].size() == 0) {
//                        continue;
//                    }
//                    List<String> association = new ArrayList<>();
//                    for (int j = 0; j < list[i].size(); j++) {
//                        association.add(idname.get(list[i].get(j)));
//                    }
//                    allasslist.add(association);
//                }
//            } catch (Exception e) {
//
//            }
//            log.info("获取的社团个数" + allasslist.size());
//            List<NodesPmid> nodesList = new ArrayList<>();
//            List<Categoris> categorisList = new ArrayList<>();
//            List<Links> linksList = new ArrayList<>();
//
//            // https://echarts.apache.org/examples/zh/editor.html?c=graph-label-overlap
//            Map<String, Integer> categorisNum = new HashMap<>();
//            //这里面存放社团中心点的坐标 用于提供后node点生成附近坐标提供参照
//            Map<Integer, NodesPmid> categorisNumXy = new HashMap<>();
//            List<NodesPmid> geneyuan = Arith.geneyuan(allasslist.size());
//            //设置每个社团区间范围 最大x712.18353++最小x-516.55884++最大y++522.4031++最小y++-493.57944 源自
//            for (int i = 0; i < allasslist.size(); i++) {
//                List<String> strings = allasslist.get(i);
//                NodesPmid nodesPmid = geneyuan.get(i);
//                categorisNumXy.put(i, nodesPmid);
//                for (String string : strings) {
//                    categorisNum.put(string, i);
//                }
//            }
//
//            Random random = new Random();
//            for (Map.Entry<String, Set<Integer>> stringSetEntry : authorSetMap.entrySet()) {
//                NodesPmid nodes = new NodesPmid();
//                nodes.setName(stringSetEntry.getKey());
//                Integer integer = categorisNum.get(stringSetEntry.getKey());
//                NodesPmid nodesPmid = categorisNumXy.get(integer);
//                if (nodesPmid != null) {
//                    // 2023年12月15日15:27:08 给这个点个坐标 在蔟附近
//                    nodes.setCategory(categorisNum.get(stringSetEntry.getKey()) == null ? 0 : categorisNum.get(stringSetEntry.getKey()));
//                    nodes.setSymbolSize(authorSetMap.get(stringSetEntry.getKey()) == null ? 0 : authorSetMap.get(stringSetEntry.getKey()).size());
//                    // 动态生成x和y 进行适配echarts
//                    NodesPmid nodesPmid1 = Arith.getsinIndex(nodesPmid.getX(), nodesPmid.getY(), random);
//                    nodes.setX(nodesPmid1.getX());
//                    nodes.setY(nodesPmid1.getY());
//                    nodes.setPmid(authorSetMap.get(stringSetEntry.getKey()));
//                    nodesList.add(nodes);
//                }
//
//            }
//            //2020年9月27日13:58:47 左侧过滤采用人名 而不是用author 代替
//            //2020年9月28日09:43:27 添加数量显示和 小于3的不显示
//            // 去掉数量显示
//            for (int i = 0; i < allasslist.size(); i++) {
//                Categoris categoris = new Categoris();
//                List<String> list1 = allasslist.get(i);
//                String author = backMaxAuthor(list1, authorSetMap);
////            String authorNum = authorSetMap.get(author).size() + "";
////            categoris.setName(author + "(" + authorNum + ")");
//                categoris.setName(author);
//                categorisList.add(categoris);
//            }
//            for (Map.Entry<AuthorPairs, Set<Integer>> authorPairsSetEntry : authorPairsSetMap.entrySet()) {
//                auPairStringSetMap.put(authorPairsSetEntry.getKey().getZuHe(), authorPairsSetEntry.getValue());
//                Links links = new Links();
//                links.setSource(authorPairsSetEntry.getKey().getFirstAu());
//                links.setTarget(authorPairsSetEntry.getKey().getSecondAu());
//                links.setName(authorPairsSetEntry.getKey().getZuHe());
//                linksList.add(links);
//            }
//            LinksCategorisPmid linksCategoris = new LinksCategorisPmid(categorisList, linksList, nodesList);
//            Associationmd5 associationmd5 = new Associationmd5();
//            associationmd5.setAuthorSetMap(authorSetMap);
//            associationmd5.setAuPairStringSetMap(auPairStringSetMap);

    /// /        RedisUtil.saveObject(md5Key, associationmd5);
    /// /        linksCategoris.setRekey(md5Key);
//            return linksCategoris;
//
//        } else {
//            return null;
//        }
//
//
//    }
    public List<MostObject> yeararticletrendTAB(String rekey) {
        String path = "/home/pubmedr/R/yeararticletrendTAB.xlsx";
        List<MostObject> resList = new ArrayList<>();
        if (StringUtil.isNotNull(rekey)) {
            String redisPath = getRedisPath(rekey);
            if (StringUtil.isNotNull(redisPath)) {
                path = path.replace("/home/pubmedr/R/", redisPath);
            } else {
                log.error("使用了默认路径++" + path);
            }
        }
        if (FileUtil.exist(path)) {
            ExcelReader read = ExcelUtil.getReader(path);
            List<Map<String, Object>> maps = read.readAll();
            for (Map<String, Object> res : maps) {
                MostObject mostObject = new MostObject();
                mostObject.setLable(res.get("PY").toString());
                mostObject.setSize(Integer.parseInt(res.get("nsize").toString()));
                mostObject.setPmids(getPmids(res.get("PMIDS").toString()));
                resList.add(mostObject);
            }
        }
        return resList;

    }

    public List<ListMostObject> sourceGrowth(String rekey) {
        String path = "/home/pubmedr/R/Source Growth.xlsx";
        List<MostObject> resList = new ArrayList<>();
        List<ListMostObject> lres = new ArrayList<>();
        if (StringUtil.isNotNull(rekey)) {
            String redisPath = getRedisPath(rekey);
            if (StringUtil.isNotNull(redisPath)) {
                path = path.replace("/home/pubmedr/R/", redisPath);
            } else {
                log.error("使用了默认路径++" + path);
            }
        }
        if (FileUtil.exist(path)) {
            ExcelReader read = ExcelUtil.getReader(path);
            List<Map<String, Object>> maps = read.readAll();
            for (Map<String, Object> res : maps) {
                MostObject mostObject = new MostObject();
                mostObject.setLable(res.get("Year").toString());
                mostObject.setSize(Integer.parseInt(res.get("value").toString()));
                mostObject.setPmids(getPmids(""));
                mostObject.setLabelo1(res.get("variable").toString());
                resList.add(mostObject);
            }
            Map<String, List<MostObject>> resMap = new LinkedHashMap<>();
            for (MostObject mostObject : resList) {
                resMap.compute(mostObject.getLabelo1(), (k, v) -> {
                    if (v == null) {
                        v = new ArrayList<>();
                    }
                    v.add(mostObject);
                    return v;
                });
            }
            resMap.forEach((k, v) -> {
                ListMostObject ressin = new ListMostObject();
                ressin.setName(k);
                ressin.setLs(v);
                lres.add(ressin);
            });
        }

        return lres;

    }

    public List<ListMostObject> IdGrowth(String rekey) {
        String path = "/home/pubmedr/R/DFKeywordGrowth.xlsx";
        List<MostObject> resList = new ArrayList<>();
        List<ListMostObject> lres = new ArrayList<>();
        if (StringUtil.isNotNull(rekey)) {
            String redisPath = getRedisPath(rekey);
            if (StringUtil.isNotNull(redisPath)) {
                path = path.replace("/home/pubmedr/R/", redisPath);
            } else {
                log.error("使用了默认路径++" + path);
            }
        }
        if (FileUtil.exist(path)) {
            ExcelReader read = ExcelUtil.getReader(path);
            List<Map<String, Object>> maps = read.readAll();
            for (Map<String, Object> res : maps) {
                MostObject mostObject = new MostObject();
                mostObject.setLable(res.get("Year").toString());
                mostObject.setSize(Integer.parseInt(res.get("value").toString()));
                mostObject.setLabelo1((res.get("keyword").toString()));
                mostObject.setPmids(getPmids(""));
                resList.add(mostObject);
            }
            Map<String, List<MostObject>> resMap = new LinkedHashMap<>();
            for (MostObject mostObject : resList) {
                resMap.compute(mostObject.getLabelo1(), (k, v) -> {
                    if (v == null) {
                        v = new ArrayList<>();
                    }
                    v.add(mostObject);
                    return v;
                });
            }
            resMap.forEach((k, v) -> {
                ListMostObject ressin = new ListMostObject();
                ressin.setName(k);
                ressin.setLs(v);
                lres.add(ressin);
            });
        }
        return lres;

    }

    public Map<String, String> otherpic(String rekey) {

        Map<String, String> resultMap = new HashMap<>();
        if (StringUtil.isNotNull(rekey)) {
            String redisPath = getRedisPath(rekey);
            log.info("path++" + redisPath);
            List<File> hwImageall = getImageAll(redisPath);
            int imageIndex = 1;
            for (File file : hwImageall) {
                // /img/getNHANESImg?finalPath=/home/jar/zxjar/paper_helper/userdata/0/68/&fileName=figS2.tiff
                String pathFile = "/img/getNHANESImg?finalPath=" + file.getParent() + "/&fileName=" + file.getName();
                resultMap.put("image" + imageIndex, pathFile);
                imageIndex++;
            }
            List<File> htmlAll = getHtmlAll(redisPath);
            if (htmlAll.size() > 0) {
                for (int i = 1; i <= htmlAll.size(); i++) {
                    File s = htmlAll.get(i - 1);
                    log.info("s++" + s);
                    String fileEnd = "baseline1".replace("1", i + "");
                    String PATH = s.getPath();
                    resultMap.put(fileEnd, FileUtil.readString(PATH, "utf-8"));
                }
            }


        }

        return resultMap;

    }

    public Object authorProdOverTime(String rekey) {
        String path = "/home/pubmedr/R/authorProdOverTime.xlsx";
        Map<String, Object> resres = new HashMap<>();
        if (StringUtil.isNotNull(rekey)) {
            String redisPath = getRedisPath(rekey);
            if (StringUtil.isNotNull(redisPath)) {
                path = path.replace("/home/pubmedr/R/", redisPath);
            } else {
                log.error("使用了默认路径++" + path);
            }
        }
        if (FileUtil.exist(path)) {
            ExcelReader read = ExcelUtil.getReader(path);
            List<Map<String, Object>> maps = read.readAll();
            // 2023年12月13日11:20:50 根据 https://echarts.apache.org/examples/zh/editor.html?c=scatter-single-axis 需要组合时间轴 作者名称 和每年发文数量 这里取被引次数进行展示 也可以才用这一年发文的数量
            Set<Integer> yearsc = new LinkedHashSet<>();
            Set<String> ausc = new LinkedHashSet<>();
            List<List<Integer>> DataSize = new ArrayList<>();
            for (Map<String, Object> res : maps) {
                try {
                    yearsc.add(Integer.parseInt(res.get("year").toString()));
                    ausc.add((res.get("Author").toString()));
                } catch (Exception e) {

                }

            }
            List<Integer> yearscLS = new ArrayList<>(yearsc);
            yearscLS = yearscLS.stream().sorted(Integer::compareTo).collect(Collectors.toList());
            List<String> ausclS = new ArrayList<>(ausc);
            Map<String, Integer> allYearIndex = new LinkedHashMap<>();
            Map<String, Integer> allAuthorIndex = new LinkedHashMap<>();
            for (int i = 0; i < yearscLS.size(); i++) {
                allYearIndex.put(yearscLS.get(i) + "", i);
            }
            for (int i = 0; i < ausclS.size(); i++) {
                allAuthorIndex.put(ausclS.get(i) + "", i);
            }
            for (String s : ausc) {
                Map<Integer, Integer> yearSize = new LinkedHashMap<>();
                for (Map<String, Object> res : maps) {
                    //res.get //  角标对应 作者 年份 因子(或者发文数量)
                    String author = res.get("Author").toString();
                    Integer year = Integer.parseInt(res.get("year").toString());
                    if (author.equals(s)) {
                        yearSize.compute(year, (k, v) -> {
                            if (v == null) {
                                v = 0;
                            }
                            v++;
                            return v;
                        });
                    }
//                    sin.add(allAuthorIndex.get(author));
//                    sin.add(allYearIndex.get(year));
//                    sin.add(Integer.parseInt(res.get("TC").toString()));
//                    DataSize.add(sin);
                }

                // 拿到了作者每年对于发文数
                yearSize.forEach((k, v) -> {
                    List<Integer> sin = new ArrayList<>();
                    sin.add(allAuthorIndex.get(s));
                    sin.add(allYearIndex.get(k + ""));
                    sin.add(v * 7);
                    DataSize.add(sin);
                });

            }
//            for (Map<String, Object> res : maps) {
//                //res.get //  角标对应 作者 年份 因子(或者发文数量)
//                List<Integer> sin = new ArrayList<>();
//                String author = res.get("Author").toString();
//                String year = res.get("year").toString();
//                sin.add(allAuthorIndex.get(author));
//                sin.add(allYearIndex.get(year));
//                sin.add(Integer.parseInt(res.get("TC").toString()));
//                DataSize.add(sin);
//            }
            resres.put("data", DataSize);
            resres.put("author", ausclS);
            resres.put("year", yearscLS);
        }


        return resres;

    }

    public List<String> getPmids(String str) {
        if (StringUtil.isNull(str)) {
            return new ArrayList<>();
        }
        List<String> allPmids = new ArrayList<>();
        String[] split = str.split(",");
        for (String s : split) {
            try {
                allPmids.add((s));
            } catch (Exception e) {

            }

        }
        return allPmids;
    }

    public String getRedisPath(String rekey) {
        Object objectByRedis = cacheUtil.getObjectByRedis(rekey + "path");
        if (objectByRedis != null) {
//            log.info("返回的路径是++" + objectByRedis.toString());
            return objectByRedis.toString();
        } else {
//            log.info("返回的路径是++" + "空");
            return "";
        }
    }

    public void geneWosExportFile(List<Entity> allInfoFromwos, String rootPath, String fileName) {

        // 导出格式为 wos 的plain text file  带参文的
        //进行组合 官方说法是需要如下字段
        //AU	Authors
        //TI	Document Title
        //SO	Publication Name (or Source)
        //JI	ISO Source Abbreviation
        //DT	Document Type
        //DE	Authors’ Keywords
        //ID	Keywords associated by SCOPUS or ISI database
        //AB	Abstract
        //C1	Author Address
        //RP	Reprint Address
        //CR	Cited References
        //TC	Times Cited
        //PY	Year
        //SC	Subject Category
        //UT	Unique Article Identifier
        //DB	Bibliographic Database
        StringBuffer newsbWos = new StringBuffer();
        String breakE = "\n";
        for (Entity allInfoFromwo : allInfoFromwos) {
            String PT = allInfoFromwo.getStr("pt");
            String AU = allInfoFromwo.getStr("au");
            String AF = allInfoFromwo.getStr("af");
            String TI = allInfoFromwo.getStr("ti");
            String SO = allInfoFromwo.getStr("so");
            String JI = allInfoFromwo.getStr("ji");
            String DT = allInfoFromwo.getStr("dt");
            String DE = allInfoFromwo.getStr("de");
            String ID = allInfoFromwo.getStr("id");
            String AB = allInfoFromwo.getStr("ab");
            String C1 = allInfoFromwo.getStr("c1");
            String C3 = allInfoFromwo.getStr("c3");
            String RP = allInfoFromwo.getStr("rp");
            String RI = allInfoFromwo.getStr("ri");
            String CR = allInfoFromwo.getStr("cr");
            String TC = allInfoFromwo.getStr("tc");
            String PY = allInfoFromwo.getStr("py");
            String SC = allInfoFromwo.getStr("sc");
            String UT = allInfoFromwo.getStr("ut");
            String NR = allInfoFromwo.getStr("nr");
            String Z9 = allInfoFromwo.getStr("z9");
            String U1 = allInfoFromwo.getStr("u1");
            String U2 = allInfoFromwo.getStr("u2");
            String PU = allInfoFromwo.getStr("pu");
            String PI = allInfoFromwo.getStr("pi");
            String PA = allInfoFromwo.getStr("pa");
            String SN = allInfoFromwo.getStr("sn");
            String EI = allInfoFromwo.getStr("ei");
            String J9 = allInfoFromwo.getStr("j9");
            String PD = allInfoFromwo.getStr("pd");
            String VL = allInfoFromwo.getStr("vl");
            String IS = allInfoFromwo.getStr("is");
            String BP = allInfoFromwo.getStr("bp");
            String EP = allInfoFromwo.getStr("ep");
            String DI = allInfoFromwo.getStr("di");
            String PG = allInfoFromwo.getStr("pg");
            String WC = allInfoFromwo.getStr("wc");
            String WE = allInfoFromwo.getStr("we");
            String GA = allInfoFromwo.getStr("ga");
            String PM = allInfoFromwo.getStr("pm");
            String OA = allInfoFromwo.getStr("oa");
            String HC = allInfoFromwo.getStr("hc");
            String HP = allInfoFromwo.getStr("hp");
            String DA = allInfoFromwo.getStr("da");
            if (StringUtil.isNotNull(PT)) {
                newsbWos.append("PT " + PT + breakE);
            }
            if (StringUtil.isNotNull(AU)) {
                String[] AUS = AU.split(";");
                for (int i = 0; i < AUS.length; i++) {
                    if (i == 0) {
                        newsbWos.append("AU " + AUS[i] + breakE);
                    } else {
                        newsbWos.append("    " + AUS[i] + breakE);
                    }
                }
            }
            if (StringUtil.isNotNull(AF)) {
                String[] AFS = AF.split(";");
                for (int i = 0; i < AFS.length; i++) {
                    if (i == 0) {
                        newsbWos.append("AF " + AFS[i] + breakE);
                    } else {
                        newsbWos.append("    " + AFS[i] + breakE);
                    }
                }
            }
            if (StringUtil.isNotNull(TI)) {
                newsbWos.append("TI " + TI + breakE);
            }
            if (StringUtil.isNotNull(SO)) {
                newsbWos.append("SO " + SO + breakE);
            }
            if (StringUtil.isNotNull(JI)) {
                newsbWos.append("JI " + JI + breakE);
            }
            if (StringUtil.isNotNull(DT)) {
                newsbWos.append("DT " + DT + breakE);
            }
            if (StringUtil.isNotNull(DE)) {
                newsbWos.append("DE " + DE + breakE);
            }
            if (StringUtil.isNotNull(ID)) {
                newsbWos.append("ID " + ID + breakE);
            }
            if (StringUtil.isNotNull(AB)) {
                newsbWos.append("AB " + AB + breakE);
            }
            if (StringUtil.isNotNull(C1)) {
                newsbWos.append("C1 " + C1 + breakE);
            }
            if (StringUtil.isNotNull(C3)) {
                newsbWos.append("C3 " + C3 + breakE);
            }
            if (StringUtil.isNotNull(RP)) {
                newsbWos.append("RP " + RP + breakE);
            }
            if (StringUtil.isNotNull(RI)) {
                newsbWos.append("RI " + RI + breakE);
            }
            if (StringUtil.isNotNull(CR)) {
                String[] CRS = CR.split(";");
                for (int i = 0; i < CRS.length; i++) {
                    if (i == 0) {
                        newsbWos.append("CR " + CRS[i] + breakE);
                    } else {
                        newsbWos.append("   " + CRS[i] + breakE);
                    }
                }
            }
            if (StringUtil.isNotNull(TC)) {
                newsbWos.append("TC " + TC + breakE);
            }
            if (StringUtil.isNotNull(PY)) {
                newsbWos.append("PY " + PY + breakE);
            }
            if (StringUtil.isNotNull(SC)) {
                newsbWos.append("SC " + SC + breakE);
            }
            if (StringUtil.isNotNull(UT)) {
                newsbWos.append("UT " + UT + breakE);
            }

            if (StringUtil.isNotNull(NR)) {
                newsbWos.append("NR " + NR + breakE);
            }
            if (StringUtil.isNotNull(Z9)) {
                newsbWos.append("Z9 " + Z9 + breakE);
            }
            if (StringUtil.isNotNull(U1)) {
                newsbWos.append("U1 " + U1 + breakE);
            }
            if (StringUtil.isNotNull(U2)) {
                newsbWos.append("U2 " + U2 + breakE);
            }
            if (StringUtil.isNotNull(PU)) {
                newsbWos.append("PU " + PU + breakE);
            }
            if (StringUtil.isNotNull(PI)) {
                newsbWos.append("PI " + PI + breakE);
            }
            if (StringUtil.isNotNull(PA)) {
                newsbWos.append("PA " + PA + breakE);
            }
            if (StringUtil.isNotNull(SN)) {
                newsbWos.append("SN " + SN + breakE);
            }
            if (StringUtil.isNotNull(EI)) {
                newsbWos.append("EI " + EI + breakE);
            }
            if (StringUtil.isNotNull(J9)) {
                newsbWos.append("J9 " + J9 + breakE);
            }
            if (StringUtil.isNotNull(PD)) {
                newsbWos.append("PD " + PD + breakE);
            }
            if (StringUtil.isNotNull(VL)) {
                newsbWos.append("VL " + VL + breakE);
            }
            if (StringUtil.isNotNull(IS)) {
                newsbWos.append("IS " + IS + breakE);
            }
            if (StringUtil.isNotNull(BP)) {
                newsbWos.append("BP " + BP + breakE);
            }
            if (StringUtil.isNotNull(EP)) {
                newsbWos.append("EP " + EP + breakE);
            }
            if (StringUtil.isNotNull(DI)) {
                newsbWos.append("DI " + DI + breakE);
            }
            if (StringUtil.isNotNull(PG)) {
                newsbWos.append("PG " + PG + breakE);
            }
            if (StringUtil.isNotNull(WC)) {
                newsbWos.append("WC " + WC + breakE);
            }
            if (StringUtil.isNotNull(WE)) {
                newsbWos.append("WE " + WE + breakE);
            }
            if (StringUtil.isNotNull(GA)) {
                newsbWos.append("GA " + GA + breakE);
            }
            if (StringUtil.isNotNull(PM)) {
                newsbWos.append("PM " + PM + breakE);
            }
            if (StringUtil.isNotNull(OA)) {
                newsbWos.append("OA " + OA + breakE);
            }
            if (StringUtil.isNotNull(HC)) {
                newsbWos.append("HC " + HC + breakE);
            }
            if (StringUtil.isNotNull(HP)) {
                newsbWos.append("HP " + HP + breakE);
            }
            if (StringUtil.isNotNull(DA)) {
                newsbWos.append("DA " + DA + breakE);
            }
            newsbWos.append("ER" + "\n");
            newsbWos.append("\n");
        }
        FileUtil.writeUtf8String(newsbWos.toString(), rootPath + fileName);
    }

}
